﻿
Imports System.Runtime.CompilerServices
Imports osi.root.utils
Imports osi.root.constants
Imports osi.root.connector
Imports osi.root.formation

Public Module _qless2
    <Extension()> Public Function push(Of T)(ByVal q As qless2(Of T),
                                             ByVal d As T,
                                             ByVal max_size As Int64,
                                             ByVal timeout_ms As Int64) As event_comb
        Dim r As pointer(Of Boolean) = Nothing
        Return New event_comb(Function() As Boolean
                                  If q Is Nothing OrElse
                                     max_size <= 0 Then
                                      Return False
                                  Else
                                      r = New pointer(Of Boolean)()
                                      Return waitfor(Function() q.push(d, max_size), r, timeout_ms) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return (+r) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function push(Of T)(ByVal q As qless2(Of T),
                                             ByVal d As T,
                                             ByVal max_size As Int64) As event_comb
        Return push(q, d, max_size, npos)
    End Function

    Private Function pop_to_pointer(Of T)(ByVal q As qless2(Of T),
                                          ByVal r As pointer(Of T)) As Boolean
        assert(Not q Is Nothing)
        Dim d As T = Nothing
        Return q.pop(d) AndAlso eva(r, d)
    End Function

    <Extension()> Public Function pop(Of T)(ByVal q As qless2(Of T),
                                            ByVal r As pointer(Of T),
                                            ByVal timeout_ms As Int64) As event_comb
        Dim pr As pointer(Of Boolean) = Nothing
        Return New event_comb(Function() As Boolean
                                  If q Is Nothing Then
                                      Return False
                                  ElseIf pop_to_pointer(q, r) Then
                                      Return goto_end()
                                  Else
                                      pr = New pointer(Of Boolean)()
                                      Return waitfor(Function() pop_to_pointer(q, r),
                                                     pr,
                                                     timeout_ms) AndAlso
                                      goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return (+pr) AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function pop(Of T)(ByVal q As qless2(Of T),
                                            ByVal r As pointer(Of T)) As event_comb
        Return pop(q, r, npos)
    End Function
End Module
